package com.hbnu.controller;

import com.hbnu.pojo.Request;
import com.hbnu.pojo.Result;
import com.hbnu.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@CrossOrigin(origins = "*")
@Slf4j
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/addToCart")
    public Result addToCart(@RequestBody Request rq){
        try{
            cartService.addToCart(rq);
            return Result.success();
        }catch (Exception exception){
            return Result.error("重复添加");
        }
    }

    @PostMapping("/delFromCart")
    public Result delFromCart(@RequestBody Request rq){
        cartService.delFromCart(rq);
        return Result.success();
    }

    @PostMapping("/getCollectionList")
    public Result getCollectionList(@RequestBody Request rq){
        return Result.success(cartService.getCollectionList(rq));
    }

    @PostMapping("/modifyCollection")
    public Result modifyCollection(@RequestBody Request rq){
        cartService.modifyCollection(rq);
        return Result.success();
    }
}
